package edu.corn.job.demo;

import edu.corn.job.core.context.JobRunningContext;
import edu.corn.job.core.engine.mutex.MutexEnum;
import edu.corn.job.core.processor.AbstractCompositeJob;
import edu.corn.job.core.processor.AbstractPhaseJobProcessor;
import edu.corn.job.core.processor.adapter.handler.CheckAndDoneHandler;
import edu.corn.job.core.statemachine.JobStateEnum;
import jakarta.annotation.Resource;

import java.util.Collections;
import java.util.Set;

public class OrderSendCompositeProcessor extends AbstractCompositeJob {

    @Resource
    private OrderService orderService;


    public OrderSendCompositeProcessor() {
        registerChild(new ParamsCheckPhase());
        registerChild(new OrderSendPhase());
        registerChild(new MinusStockCompositeProcessor(), new CheckAndDoneHandler() {
            @Override
            public String handle(JobRunningContext context) {
                // 执行完检查是否需要重试。默认重试当前job。
                return CheckAndDoneHandler.super.handle(context);
            }
        });
    }

    @Override
    public String getName() {
        return "OrderSendProcessor";
    }

    @Override
    public Set<? extends Enum<?>> mutexes() {
        // 或者是枚举类。
        return Collections.singleton(JobStateEnum.NEW);
    }

    @Override
    public MutexEnum mutexOp() {
        return MutexEnum.PER;
    }

    private  static class ParamsCheckPhase extends AbstractPhaseJobProcessor {

        @Override
        public String getName() {
            return "ParamsCheckPhase";
        }

        @Override
        public void execute(JobRunningContext context) {
            Object userinfo = context.getParam("userinfo");
            // 参数检查
            // 风控检查
            // 库存检查
        }
    }

    private static class OrderSendPhase extends AbstractPhaseJobProcessor {


        @Override
        public String getName() {
            return "OrderSendPhase";
        }

        @Override
        public void execute(JobRunningContext context) {
            // 调用orderService执行一些列方法。
        }
    }
}
